<%@ Page language="vb" CodeFile="searchDealerEmployee.aspx.vb" Inherits="searchDealerEmployee_aspx_vb" %>
<%
    rowNumber = 0
    If Request.QueryString("do") = Nothing Then
        dealerID = Request.QueryString("dealerID")
        subDo = Request.QueryString("subDo")
        dbOpen()
        RecSet = Conn.Execute("Select dealer From tblDealer Where dealerID = " & dealerID & "")
        If Not RecSet.EOF Then
            dealer = RecSet.Fields("dealer").Value
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Over()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top' colspan='3'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
        Response.Write("<tr><td class='headline' colspan='5'>SALES MAN AT " & UCase(dealer) & "<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select d.dealerEmployeeID, d.dealerEmployee, d.pwd, (Select Count(v.vehicleID) From tblVehicle v Where v.dealerEmployeeID = d.dealerEmployeeID) As vehicleCount From tblDealerEmployee d Where d.dealerID = " & dealerID & " Order By d.dealerEmployee")
        If RecSet.EOF Then
            Response.Write("<tr><td><font color='red'>No sales man found.</font></td></tr>")
        Else
            Response.Write("<tr bgcolor='#eeeeee'>")
            Response.Write("<td class='formBold'>Sales man</td>")
            Response.Write("<td class='formBold'>username</td>")
            Response.Write("<td class='formBold'>Password</td>")
            Response.Write("<td class='formBold'>Number of lease contracts</td>")
            Response.Write("<td class='formBold'></td>")
            Response.Write("</tr>")
            Do Until RecSet.EOF
                dealerEmployeeID = RecSet.Fields("dealerEmployeeID").Value
                dealerEmployee = RecSet.Fields("dealerEmployee").Value
                pwd = RecSet.Fields("pwd").Value
                vehicleCount = RecSet.Fields("vehicleCount").Value
                If IsDBNull(pwd) = True Then
                    pwd = "N/A"
                End If
                If rowNumber = 0 Then
                    strGrey = ""
                    rowNumber = 1
                Else
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 0
                End If
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & dealerEmployee & "</td>")
                Response.Write("<td>de" & dealerEmployeeID & "</td>")
                Response.Write("<td>" & pwd & "</td>")
                Response.Write("<td>" & vehicleCount & "</td>")
                Response.Write("<td align='right'><a href='searchDealerEmployee.aspx?subDo=viewVehicles&dealerID=" & dealerID & "&dealerEmployeeID=" & dealerEmployeeID & "'>view lease contracts</a></td>")
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End If
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='5'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        If subDo = "viewVehicles" Then
            Response.Write("<tr>")
            Response.Write("<td valign='top' colspan='3'>")
            Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
            Response.Write("<tr><td class='headline' colspan='12'>LEASE CONTRACTS<hr class='hrHeadline'></td></tr>")
            dbOpen()
            RecSet = Conn.Execute("Select v.vehicleID, v.regnr, v.vinNumber, v.contractNumber, v.date AS tblVehicleDate, p.product, b.brand, m.model, c.country From ((((tblVehicle v INNER JOIN tblProduct p ON p.productID = v.productID) INNER JOIN tblBrand b ON b.brandID = v.brandID) INNER JOIN tblModel m ON m.modelID = v.modelID) INNER JOIN tblCountry c ON c.countryID = v.countryID) Where v.dealerEmployeeID = " & dealerEmployeeID & " ORDER BY v.vehicleID")
            If RecSet.EOF Then
                Response.Write("<tr><td><font color='red'>No lease contracts found.</font></td></tr>")
            Else
                Response.Write("<tr bgcolor='#eeeeee'>")
                Response.Write("<td class='formBold'>Contract number</td>")
                Response.Write("<td class='formBold'>Licence number</td>")
                Response.Write("<td class='formBold'>VIN-number</td>")
                Response.Write("<td class='formBold'>Car make</td>")
                Response.Write("<td class='formBold'>Product</td>")
                Response.Write("<td class='formBold'>Country</td>")
                Response.Write("<td class='formBold'>First created</td>")
                Response.Write("<td class='formBold'></td>")
                Response.Write("</tr>")
                rowNumber = 0
                Do Until RecSet.EOF
                    vehicleID = RecSet.Fields("vehicleID").Value
                    regnr = RecSet.Fields("regnr").Value
                    vinNumber = RecSet.Fields("vinNumber").Value
                    contractNumber = RecSet.Fields("contractNumber").Value
                    tblVehicleDate = RecSet.Fields("tblVehicleDate").Value
                    product = RecSet.Fields("product").Value
                    brand = RecSet.Fields("brand").Value
                    model = RecSet.Fields("model").Value
                    country = RecSet.Fields("country").Value
                    If rowNumber = 0 Then
                        strGrey = ""
                        rowNumber = 1
                    Else
                        strGrey = " bgcolor='#eeeeee'"
                        rowNumber = 0
                    End If
                    Response.Write("<tr" & strGrey & ">")
                    Response.Write("<td>" & contractNumber & "</td>")
                    Response.Write("<td>" & regnr & "</td>")
                    Response.Write("<td>" & vinNumber & "</td>")
                    Response.Write("<td>" & brand & " " & model & "</td>")
                    Response.Write("<td>" & product & "</td>")
                    Response.Write("<td>" & country & "</td>")
                    Response.Write("<td>" & Left(Today(), 10) & "</td>")
                    Response.Write("<td align='right'><a href='editVehicle.aspx?vehicleID=" & vehicleID & "'>view details</a></td>")
                    Response.Write("</tr>")
                    RecSet.MoveNext()
                Loop
            End If
            RecSet.Close()
            RecSet = Nothing
            dbClose()
            Response.Write("<tr><td colspan='12'><hr class='hrHeadline'></td></tr>")
            Response.Write("</table>")
            Response.Write("</td>")
            Response.Write("</tr>")
        End If
        Response.Write("</table>")
        Under()
    End If
%>
